//获取页面开始时间，添加在HTML前
window.startTime = +new Date();

//获取浏览器开始渲染时间
(function(){
                var timeStart = + new Date,
                    limit = 1,
                    timer = setInterval(function(){
                    if((document.body&&document.body.scrollHeight > 0) || (limit++ == 500)){
						clearInterval(timer);
                        window.startRenderTime = +new Date();
						window.startRenderTimelimit = limit;
                    }
                },10);
})()


//获取domReady时间
var dom = [];
dom.isReady = false; //用于判断页面是否加载完毕
dom.ready = function(fn){
	if ( dom.isReady ){
		fn();
	} else {
		dom.push( fn );
	}
};

//执行所有在window.onload之前放放的函数
dom.fireReady = function(){
	if( !dom.isReady ){
		if ( !document.body ){
			return setTimeout( dom.fireReady, 16 );
		}
		dom.isReady = 1;
		if ( dom.length ){
			for( var i = 0, fn; fn = dom[i]; i++)
				fn();
		}
	}
};

//在开始初始化domReady函数，判断页面的加载情况
if( document.readyState == 'complete' ) {
	dom.fireReady();
} else if( -[1,] ){
	document.addEventListener('DOMContentLoaded', function(){
		document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
		dom.fireReady();
	}, false);
} else {
//当页面包含图片时，onreadystatechange事件会在触发window.onload之后
//也就是说：它只能正确地执行于页面不包含二进制资源或非常少的或者被缓存时
	document.attachEvent('onreadystatechange', function(){
		if( document.readyState == "complete" ) {
			document.detachEvent('onreadystatechange', arguments.callee);
			dom.fireReady();
		}
	});
}

(function(){
	if ( dom.isReady ) {
		return;
	}

	//doScroll存在于所有标签而不管其是否支持滚动条
	//这里如果用document.documentElement.doScroll(),我们需要判定其是否位于顶层document
	var node = new Image;
	try{
		node.doScroll();
		node = null; //防止IE内存泄漏
	}catch(e){
		setTimeout(arguments.callee, 64);
		return;
	}
	dom.fireReady();


})();

function getDomReadyTime(){
	window.domReadyTime =  +new Date();
}
dom.ready(getDomReadyTime);

//获取页面onload时间
window.onload = function(){
	window.onloadTime = +new Date();

	console.log('开始时间:'+ window.startTime +', 开始渲染时间:'+ window.startRenderTime +' || '+window.startRenderTimelimit+', domReady:'+ window.domReadyTime + ',onload时间:'+window.onloadTime)
}
